home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / p_aa4re / bb212src / bbwakeup.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-01-12  |  13.0 KB  |  353 lines

  1. (*===========================================================================*)
  2. (* WAKEUP main line                                                          *)
  3. (*                                                                           *)
  4. (*   Copyright 1988, 1989, 1990, 1991 by H. Roy Engehausen.  All rights      *)
  5. (*   reserved.                                                               *)
  6. (*                                                                           *)
  7. (*===========================================================================*)
  8.  
  9. {$O+}
  10.  
  11. UNIT BBWAKEUP;
  12.  
  13. INTERFACE
  14.  
  15. PROCEDURE wakeup;
  16.  
  17. VAR
  18.   wakeup_shut_sw : BOOLEAN;
  19.  
  20. IMPLEMENTATION
  21.  
  22. USES
  23.   DOS,
  24.   bbdummy,
  25.   bbmess,
  26.   bbmisci,
  27.   bbmisc5,
  28.   bbsdata,
  29.   bbsema2,
  30.   bbstr,
  31.   bbtime,
  32.   bbucmd,
  33.   bbwin;
  34.  
  35. PROCEDURE wakeup;
  36.  
  37.   VAR
  38.     date_ok         : BOOLEAN;
  39.     did_today       : BOOLEAN;
  40.     event_time      : LONGINT;
  41.     i               : BYTE;
  42.     last_time       : LONGINT;
  43.     line_buffer     : STRING;
  44.     line_no         : WORD;
  45.     line_no_str     : STRING[5];
  46.     midnight        : LONGINT;
  47.     small_time      : LONGINT;
  48.     wakeup_count    : WORD;
  49.     wakeup_in       : TEXT;
  50.     wakeup_out      : TEXT;
  51.     wakeup_temp     : STRING;
  52.  
  53.   {$I BBWAKEPL}
  54.  
  55.   PROCEDURE wakeup_cleanup;
  56.  
  57.     VAR
  58.       t : LONGINT;
  59.  
  60.     BEGIN;
  61.  
  62.       {$I-}
  63.       CLOSE(wakeup_in);
  64.       CLOSE(wakeup_out);
  65.       {$I+}
  66.  
  67.       t := current_day_time + small_time;
  68.  
  69.       IF wakeup_time < t THEN
  70.         wakeup_time := t;
  71.  
  72.     END;
  73.  
  74.   BEGIN;
  75.  
  76.     (*-----------------------------------------------------------------------*)
  77.     (* Initialize                                                            *)
  78.     (*-----------------------------------------------------------------------*)
  79.  
  80.     active_port             := @dummy_port;
  81.     active_tcb^.tcb_port    := @dummy_port;
  82.  
  83.     active_tcb^.port_chan_s := 'WK';
  84.     active_tcb^.error_sw    := FALSE;
  85.  
  86.     small_time := opt_block.wakeup_intervl + 1;
  87.     small_time := small_time DIV secs_per_tick;
  88.  
  89.     (*-----------------------------------------------------------------------*)
  90.     (* Tell user we are running                                              *)
  91.     (*-----------------------------------------------------------------------*)
  92.  
  93.     send_tnc_data_str('WAKEUP processing started' + cr);
  94.  
  95.     (*-----------------------------------------------------------------------*)
  96.     (* Set default wakeup time to midnight                                   *)
  97.     (*-----------------------------------------------------------------------*)
  98.  
  99.     wakeup_time := last_midnight + ticks_per_day;
  100.  
  101.     (*-----------------------------------------------------------------------*)
  102.     (* Obtain the interrupt lock                                             *)
  103.     (*-----------------------------------------------------------------------*)
  104.  
  105.     get_semaphore(semaphore_interrupts, sem_exclusive, FALSE);
  106.  
  107.     (*-----------------------------------------------------------------------*)
  108.     (* Open wakeup input file                                                *)
  109.     (*-----------------------------------------------------------------------*)
  110.  
  111.     ASSIGN(wakeup_in,  opt_block.wakeup_fn);
  112.  
  113.     {$I-}
  114.     RESET(wakeup_in);
  115.     i := IORESULT;
  116.     {$I+}
  117.  
  118.     (*-----------------------------------------------------------------------*)
  119.     (* Free the interrupt lock                                               *)
  120.     (*-----------------------------------------------------------------------*)
  121.  
  122.     free_semaphore(semaphore_interrupts);
  123.  
  124.     (*-----------------------------------------------------------------------*)
  125.     (* Handle open errors                                                    *)
  126.     (*-----------------------------------------------------------------------*)
  127.  
  128.     IF i = 2 THEN
  129.       BEGIN;
  130.         send_tnc_data_str('WAKEUP file not found' + cr);
  131.         EXIT;
  132.       END;
  133.  
  134.     IF i <> 0 THEN
  135.       BEGIN;
  136.         send_tnc_data_str('I/O error on WAKEUP input file' + cr);
  137.         send_tnc_data_str(dos_err_message(i) + cr);
  138.         EXIT;
  139.       END;
  140.  
  141.     (*-----------------------------------------------------------------------*)
  142.     (* Obtain the interrupt lock                                             *)
  143.     (*-----------------------------------------------------------------------*)
  144.  
  145.     get_semaphore(semaphore_interrupts, sem_exclusive, FALSE);
  146.  
  147.     (*-----------------------------------------------------------------------*)
  148.     (* Open wakeup output file                                               *)
  149.     (*      Note:  The last two variables on the FSPLIT are "don't care"     *)
  150.     (*-----------------------------------------------------------------------*)
  151.  
  152.     FSPLIT(opt_block.wakeup_fn, wakeup_temp, line_buffer, line_no_str);
  153.  
  154.     wakeup_temp := wakeup_temp + 'WAKEUP.TMP';
  155.  
  156.     ASSIGN(wakeup_out, wakeup_temp);
  157.  
  158.     {$I-}
  159.     REWRITE(wakeup_out);
  160.     i := IORESULT;
  161.     {$I+}
  162.  
  163.     (*-----------------------------------------------------------------------*)
  164.     (* Free the interrupt lock                                               *)
  165.     (*-----------------------------------------------------------------------*)
  166.  
  167.     free_semaphore(semaphore_interrupts);
  168.  
  169.     (*-----------------------------------------------------------------------*)
  170.     (* Handle open errors                                                    *)
  171.     (*-----------------------------------------------------------------------*)
  172.  
  173.     IF i <> 0 THEN
  174.       BEGIN;
  175.         send_tnc_data_str('I/O error on WAKEUP output file' + cr);
  176.         send_tnc_data_str(dos_err_message(i));
  177.         EXIT;
  178.       END;
  179.  
  180.     (*-----------------------------------------------------------------------*)
  181.     (* Processing loop                                                       *)
  182.     (*-----------------------------------------------------------------------*)
  183.  
  184.     line_no := 0;
  185.     wakeup_did_something := FALSE;
  186.  
  187.     WHILE NOT EOF(wakeup_in) DO
  188.       BEGIN;
  189.  
  190.         (*-------------------------------------------------------------------*)
  191.         (* Bump line number and prepare error message text in case it is     *)
  192.         (* needed.                                                           *)
  193.         (*-------------------------------------------------------------------*)
  194.  
  195.         INC(line_no);
  196.         STR(line_no, line_no_str);
  197.         line_no_str := line_no_str + cr;
  198.  
  199.         (*-------------------------------------------------------------------*)
  200.         (* Read a line                                                       *)
  201.         (*-------------------------------------------------------------------*)
  202.  
  203.         READLN(wakeup_in, line_buffer);
  204.  
  205.         (*-------------------------------------------------------------------*)
  206.         (* Check for null lines and comments                                 *)
  207.         (*-------------------------------------------------------------------*)
  208.  
  209.         IF (LENGTH(line_buffer) > 0)
  210.                   AND (line_buffer[1] <> '*')
  211.                   AND (line_buffer[1] <> ';') THEN
  212.           BEGIN;
  213.  
  214.             (*---------------------------------------------------------------*)
  215.             (* Upper case the thing                                          *)
  216.             (*---------------------------------------------------------------*)
  217.  
  218.             upcase_str_var(line_buffer);
  219.  
  220.             (*---------------------------------------------------------------*)
  221.             (* Process the last date time and action date time               *)
  222.             (*---------------------------------------------------------------*)
  223.  
  224.             process_line;
  225.  
  226.             (*---------------------------------------------------------------*)
  227.             (* Do we need action?                                            *)
  228.             (*---------------------------------------------------------------*)
  229.  
  230.             IF event_time < current_day_time THEN
  231.               BEGIN;
  232.  
  233.                 (*-----------------------------------------------------------*)
  234.                 (* Action needed.  We will want to wake up a minute from now *)
  235.                 (*-----------------------------------------------------------*)
  236.  
  237.                 wakeup_time := current_day_time + small_time;
  238.  
  239.                 (*-----------------------------------------------------------*)
  240.                 (* We only do one action per wakeup                          *)
  241.                 (*-----------------------------------------------------------*)
  242.  
  243.                 IF NOT wakeup_did_something THEN
  244.                   BEGIN;
  245.  
  246.                     (*-------------------------------------------------------*)
  247.                     (* Assume we did it                                      *)
  248.                     (*-------------------------------------------------------*)
  249.  
  250.                     wakeup_did_something := TRUE;
  251.  
  252.                     (*-------------------------------------------------------*)
  253.                     (* First action this wakeup cycle.  So do it             *)
  254.                     (*-------------------------------------------------------*)
  255.  
  256.                     wakeup_temp := subword(@line_buffer, 3, 0);
  257.  
  258.                     send_tnc_data_str('Executing WAKEUP -- Line #'
  259.                                                                 + line_no_str);
  260.                     send_tnc_data_str('Command: ' + wakeup_temp + cr);
  261.  
  262.                     user_command(wakeup_temp);
  263.  
  264.                     (*-------------------------------------------------------*)
  265.                     (* Set last time so we know we did it                    *)
  266.                     (*-------------------------------------------------------*)
  267.  
  268.                     IF wakeup_did_something THEN
  269.                       last_time := current_day_time;
  270.  
  271.                   END
  272.  
  273.                 ELSE
  274.  
  275.                   (*---------------------------------------------------------*)
  276.                   (* Action needed later.  Wakeup one minute                 *)
  277.                   (*---------------------------------------------------------*)
  278.  
  279.                   wakeup_time := current_day_time + small_time;
  280.  
  281.               END
  282.             ELSE
  283.               BEGIN;
  284.  
  285.                 (*-----------------------------------------------------------*)
  286.                 (* Not time for this action.  See if we need to move up the  *)
  287.                 (* wakeup time                                               *)
  288.                 (*-----------------------------------------------------------*)
  289.  
  290.                 IF event_time < wakeup_time THEN
  291.                   wakeup_time := event_time;
  292.  
  293.               END;
  294.  
  295.             (*---------------------------------------------------------------*)
  296.             (* Prepare the output buffer                                     *)
  297.             (*---------------------------------------------------------------*)
  298.  
  299.             line_buffer := time_str(last_time, TRUE) + ' ' + line_buffer;
  300.  
  301.           END;
  302.  
  303.         (*-------------------------------------------------------------------*)
  304.         (* Write the line back to the WAKEUP file                            *)
  305.         (*-------------------------------------------------------------------*)
  306.  
  307.         WRITELN(wakeup_out, line_buffer);
  308.  
  309.       END; (*----- End loop reading wakeup file -----------------------------*)
  310.  
  311.     (*-----------------------------------------------------------------------*)
  312.     (* Obtain the interrupt lock                                             *)
  313.     (*-----------------------------------------------------------------------*)
  314.  
  315.     get_semaphore(semaphore_interrupts, sem_exclusive, FALSE);
  316.  
  317.     (*-----------------------------------------------------------------------*)
  318.     (* Clean things up.                                                      *)
  319.     (*-----------------------------------------------------------------------*)
  320.  
  321.     wakeup_cleanup;
  322.  
  323.     (*-----------------------------------------------------------------------*)
  324.     (* Erase the old file and rename the new one to the right name           *)
  325.     (*-----------------------------------------------------------------------*)
  326.  
  327.     ERASE(wakeup_in);
  328.     RENAME(wakeup_out, opt_block.wakeup_fn);
  329.  
  330.     (*-----------------------------------------------------------------------*)
  331.     (* Free the interrupt lock                                               *)
  332.     (*-----------------------------------------------------------------------*)
  333.  
  334.     free_semaphore(semaphore_interrupts);
  335.  
  336.     (*-----------------------------------------------------------------------*)
  337.     (* Tell user we are done                                                 *)
  338.     (*-----------------------------------------------------------------------*)
  339.  
  340.     send_tnc_data_str('WAKEUP processing ended -- Next wakeup at '
  341.                                            + time_str(wakeup_time, TRUE) + cr);
  342.  
  343.     send_flush;
  344.  
  345.     IF wakeup_shut_sw THEN
  346.       shutdown_bbs;
  347.  
  348.     EXIT;
  349.  
  350.   END;
  351.  
  352. END.
  353.